let dataHtml="";
layui.config({
    version: '1510164124181'
}).use(['form', 'laypage', 'layer', 'element'], function () {
    form = layui.form;
    layer = layui.layer;
    $ = layui.jquery;
    laypage = layui.laypage;//分页
    element = layui.element; //元素操作
    TypelistByTypeId(1);
    inituserdata();
});



function inituserdata() {
    let SelectInfo = $("#types option:selected").val();
    let InputInfo = $("#SearchInfo").val();
    dataHtml="";
    var index = top.layer.msg('加载中，请稍候', {icon: 16, time: false, shade: 0.8});
    $("#tableInfo tbody").html("");
    $.ajax({
        url: httpurl + "/callHeadLinespc",
        cache: false,headers: {
			"Access-Control-Allow-Origin": "*",
			"Access-Control-Allow-Methods": "*",
			"Access-Control-Allow-Headers": "Authorization",
			"Authorization": GetTokenInfo()
		},
		contentType : 'application/json',
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType : 'application/json',
        data: {
            "typeid": SelectInfo,
            "page": pageNo,
            "pagesize": pageRecords,
            "title": InputInfo,
        },
        success: function (data) {
            // console.log("====" + JSON.stringify(data));
            if(data.rtnCode==200) {
                let dataList = data.data.list;
                for (let i = 0; i < dataList.length; i++) {
                    totalRecords = data.data.total;
                    let choice=(dataList[i].choice==0?"取消精选":"设为精选");
                    dataHtml += '<tr>' +
                        '<td><input data-id="' + dataList[i].id + '" type="checkbox"  name="box" class="aceChild"/></td>' +
                        '<td>' + (isNotNull(dataList[i].title)?dataList[i].title:"") + '</td>' +
                        '<td><img width="80" src="' + visitimgurl + '/' + dataList[i].mastmap + '"/></td>' +
                        '<td>' + (isNotNull(dataList[i].typelist)?dataList[i].typelist.name:"") + '</td>' +
                        '<td>' + (dataList[i].choice==0?"是":"否") + '</td>' +
                        '<td>' + dataList[i].intime + '</td>' +
                        '<td><a href="#" data-id="' + dataList[i].id + '" data-info="'+dataList[i].choice+'" onclick="CancelChoice(this)">'+choice+'</a>&nbsp;&nbsp;<a href="AddActicle.html?ActicleId=' + dataList[i].id + '">编辑</a>&nbsp;&nbsp;<a href="#" data-id="' + dataList[i].id + '" data-info="deletedHeadLines" onclick="DeleteInfo(this)">删除</a></td>' +
                        '</tr>';
                }
            } else {
                totalRecords = 0;
                dataHtml += '<tr>'
                    + '<td colspan="12" style="text-align:center;">暂无数据</td>'
                    + '</tr>';
            }
            $("#tableInfo tbody").html(dataHtml);
            top.layer.closeAll();
            pageLayPage("inituserdata");
        },error:function () {
            top.layer.closeAll();
            top.layer.alert("加载失败,请检查网络设置!!")
        }

    });

}
function onloadInfo(){
    inituserdata();
}
function CancelChoice(obj) {
    let id=$(obj).attr("data-id");
    let choice=$(obj).attr("data-info");
    $.ajax({
        type:"POST",
        url: httpurl + "/CancelChoice",
        cache: false,
        headers: {
			"Access-Control-Allow-Origin": "*",
			"Access-Control-Allow-Methods": "*",
			"Access-Control-Allow-Headers": "Authorization",
			"Authorization": GetTokenInfo()
		},
		contentType : 'application/json',
        data: JSON.stringify({
            "id": id,
            "state": choice
        }),
        success: function (data) {
            top.layer.alert(data.msg,function (index) {
                top.layer.closeAll();
                inituserdata();
            })

        }

    });
}

//分页
function page() {
    laypage.render({
        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
        , count: totalRecords //数据总数，从服务端得到
        , curr: pageNo //获取起始页 location.hash.replace('#!fenye=', '')
        , hash: 'fenye' //自定义hash值
        , layout: ['count', 'prev', 'page', 'next', 'skip']
        , jump: function (obj, first) {
            //首次不执行
            if (!first) {
                pageNo = obj.curr;// 当前页数;
                pageRecords = obj.limit;// 当前页面记录数
                inituserdata();
            }
        }
    });
}

function SearchInfo() {
    pageNo = 1;
    inituserdata();
}
